package whf.framework.dao.hibernate;

import java.io.Serializable;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.Type;
import org.hibernate.util.PropertiesHelper;

import whf.framework.util.Sequence;

/**
 * 生成HIbernate id
 * @author wanghaifeng
 * 
 */
public class HibernateIdGenerator implements IdentifierGenerator, Configurable {
	/**
	 * 对于Category相同的对象在同样的id序列中生成顺序数字
	 * @property String:category
	 */
	private	String	category = "";

	public void configure(Type type, Properties params, Dialect dialect) {
		category = PropertiesHelper.getString("category", params, "hibernate_unique_key").toUpperCase();
	}

	public Serializable generate(SessionImplementor session, Object object) throws HibernateException{
		return Sequence.nextId(category);
	}
}